<?php
class TestAction extends TopAction {
	public function t1() {
		
		$page = intval ( $_GET ['p'] ) ? intval ( $_GET ['p'] ) : 1;
		$cache_key = "orders_{$page}";
		//$orders = S ( $cache_key );
		//$companys  = D("Postsetting")->viewCompanys( 1 );
		$templates = D('Postsetting')->getTemplates( 1 );
		foreach ( $templates as $template) {
			$tploptions .= "<option value='{$template['id']}'>{$template['tplname']}</option>";
		}
		$ps = D ( 'Postsetting' );
		$districts = $ps->getDistrict ();
		$out = '';
		foreach ( $districts as $dist ) {
			$selected = ( $cityid == $dist['id'] ) ? ' selected="selected"' : '';
			$district .= '<option value="'. U('Index/tradelist?did='.$dist ['id'] ) .'" '.$selected.'>' . $dist ['name'] . '</option>';
		}
		$did = intval( $_GET['did'] );
		
		
		if (empty ( $orders )) {
			//echo '::::::GET API::::::';
			$params = array (
					'method' => 'taobao.trades.sold.get',
					'session' => $this->_top_session,
					'timestamp' => date ( 'Y-m-d H:i:s' ),
					'format' => TAO_RETURN_FORMAT,
					'app_key' => TAO_APPKEY,
					'v' => '2.0',
					'sign_method' => 'md5',
					
					'fields' => 'seller_nick,buyer_nick,num,tid,buyer_message,payment,buyer_memo,seller_memo,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders,orders.pic_path,orders.sku_properties_name,orders.outer_sku_id,pic_path,seller_flag,status,created,post_fee,shipping_type',
					'buyer_nick' => '琪漆520',
					//'status' => 'WAIT_SELLER_SEND_GOODS',
					'page_no' => $page,
					'page_size' => 100
					
			);
			
			$tc = new TopClient ();
			
			$orders = $tc->getResult ( $params );
			$orders = A("Index")->filterTrades( $orders ) ;
			if( $did ) {
				$disname = $ps->getDiscrictName( $did );
			 	if( $disname ) {
					foreach( $orders['trades_sold_get_response']['trades']['trade'] as $k=>$tmporder ) {
						if( $tmporder['receiver_state'] != $disname ) {
							unset( $orders['trades_sold_get_response']['trades']['trade'][$k] );
						}
					}
					
				}
			}
			foreach( $orders['trades_sold_get_response']['trades']['trade'] as $k=>$tmporder ) {
				$session_trade[ number_format( $tmporder['tid'] , 0, '', '' ) ] = $tmporder ;
			}
			$_SESSION['trade'] = $session_trade;
			unset( $session_trade );
			//获取订单详情*********************************************
			foreach( $orders['trades_sold_get_response']['trades']['trade'] as $k=>$tmporder ) {
				//$tids .= $tmporder['tid'].';';
				$fullinfo = array();
				for( $i=0;$i<3;$i++) {
					$params = array (
							'method' => 'taobao.trade.fullinfo.get',
							'session' => $this->_top_session,
							'timestamp' => date ( 'Y-m-d H:i:s' ),
							'format' => TAO_RETURN_FORMAT,
							'app_key' => TAO_APPKEY,
							'v' => '2.0',
							'sign_method' => 'md5',
								
							'fields' => 'buyer_message,seller_memo',
							'tid' => $tmporder['tid']
					);
					$fullinfo = $tc->getResult( $params );
					Log::write( 'fullinfo.get::::'. str_replace( PHP_EOL, ' ',  var_export( $fullinfo, true) ).'_i:'.$i.'_tid:'.$tmporder['tid'].'_user:'.$_SESSION['seller_nick'], LOG::INFO , '', LOG_PATH.date('y_m_d').'.fullinfo.log' );
					if(! empty( $fullinfo ) ) break;
				}
					
				$orders['trades_sold_get_response']['trades']['trade'][$k]['buyer_message'] = 	$fullinfo['trade_fullinfo_get_response']['trade']['buyer_message'];
				$orders['trades_sold_get_response']['trades']['trade'][$k]['seller_memo'] = 	$fullinfo['trade_fullinfo_get_response']['trade']['seller_memo'];
			}
			//获取订单详情end*********************************************
			
			
			//S ( $cache_key, $orders, 180 );
			foreach( $orders['trades_sold_get_response']['trades']['trade'] as $trade ) {
				$tradeTmp[  number_format( $trade['tid'], 0, '', '' ) ] = $trade['receiver_name'] .','. $trade['receiver_mobile'] . ','. $trade['receiver_phone'] .',' . $trade['receiver_state'] .', '. $trade['receiver_city']. ' '. $trade['receiver_district'] .','. $trade['receiver_address'] .','. $trade['receiver_zip'];
				$tradeTmp2[  number_format( $trade['tid'], 0, '', '' ) ] = $trade;
			}
			
// 			print_r( $tradeTmp );
			$trade_count = array_count_values( $tradeTmp );
// 			print_r( $trade_count );
			foreach ( $trade_count as $tkey => $tcount ) {
				if( $tcount === 1 ) continue;
				$trade_mergeTmp[] = $tkey;
			}

			if( $trade_mergeTmp ) { 
				foreach ( $trade_mergeTmp as $tmt ){
					$trade_merge[] = array_keys( $tradeTmp , $tmt  );
				}
			}
			//$tradeTmp
			//$tm  tid1  tid2
			//print_r( $trade_merge );
			foreach( $trade_merge as $k=>$tm ){
				$tidf = $tm[0];
				$tmids = '';
				$tmPayment = 0;
				$tmNum = 0;
				$trade_mergeR[$k] = $tradeTmp2[ $tidf ];
				$tmOrder = array( );
				$tmBuyer_message = $tmBuyer_message = '';
				$tmPost_fee = $tmShiping_type = '';
				foreach( $tm as $kk=>$t){					
					$tmOrder = array_merge( $tmOrder , $tradeTmp2[$t]['orders']['order'] );
					$tmids 	.= $t .',';
					$tmPayment 	+= $tradeTmp2[$t]['payment'];
					$tmSeller_memo .= $tradeTmp2[$t]['seller_memo'] ? 		$tradeTmp2[$t]['seller_memo'].';' : '' ;
					$tmBuyer_message .= $tradeTmp2[$t]['buyer_message'] ? 	$tradeTmp2[$t]['buyer_message'].';' : '' ;
					$tmPost_fee .= $tradeTmp2[$t]['post_fee'] ? 			$tradeTmp2[$t]['post_fee'].';<br>' : '' ;
					$tmShiping_type .= $tradeTmp2[$t]['shipping_type'] ? 	shipping_type ( $tradeTmp2[$t]['shipping_type'] ).';<br>' : '' ;
				}
				$trade_mergeR[$k]['payment'] 	= $tmPayment;
				$trade_mergeR[$k]['orders']['order'] = $tmOrder;
				$trade_mergeR[$k]['seller_memo'] 	= $tmSeller_memo;
				$trade_mergeR[$k]['buyer_message'] 	= $tmBuyer_message;
				$trade_mergeR[$k]['post_fee'] 	= $tmPost_fee;
				$trade_mergeR[$k]['shipping_type'] 	= $tmShiping_type;
				
				$trade_mergeR[$k]['tids'] = trim($tmids , ',' );
				$trade_mergeR[$k]['tids'] = trim($tmids , ',' );
			}
			//print_r ( $trade_mergeTmp );
			
			$_SESSION['trade_merge'] = $trade_mergeR;
			unset( $trade_merge );
			unset( $tradeTmp );
			unset( $tradeTmp2 );
			unset( $tradeTmp2 );
		}
		if( $trade_mergeR ) {
			//把在合并订单的ID过滤掉
			$orders = A("Index")->filterMergeTrade( $orders, $trade_mergeR );
		}
		
		//退款的
		
		$params = array (
				'method' => 'taobao.refunds.receive.get',
				'session' => $this->_top_session,
				'timestamp' => date ( 'Y-m-d H:i:s' ),
				'format' => TAO_RETURN_FORMAT,
				'app_key' => TAO_APPKEY,
				'v' => '2.0',
				'sign_method' => 'md5',
					
				'fields' => 'tid',
				'page_size' => 100,
		);
			
		for( $i=0;$i<3;$i++) { 
			$refounds = $tc->getResult ( $params );
			if(! empty( $refounds ) ) break;
		}
		if( $refounds ['refunds_receive_get_response'] ['total_results'] > 0  ) {
			foreach( $refounds ['refunds_receive_get_response'] ['refunds'] ['refund']  as $refund ) {
				$refundTrade[ $refund['tid'] ] = $refund ;
			}
		}
		
		//{"refunds_receive_get_response":{"total_results":0}}
		
		$count = $orders['trades_sold_get_response']['total_results'];
		import ( "ORG.Util.Page" ); // 导入分页类
		$p = new Page ( $count, 100 );
		$page = $p->show ();
		
		
		$this->assign( 'tploptions', $tploptions );
		$this->assign( 'district', $district );
		
		
		$this->assign ( "page", $page );
		$this->assign( 'tploptions', $tploptions );
		$this->assign ( 'orders', $orders );
		$this->assign( 'trade_mergeR' , $trade_mergeR );	//合并的订单
		//$this->assign( 'tradeTmp' , $tradeTmp );
		
		$this->assign('did', $did );
		
		$this->assign( 'refund' , $refundTrade ) ;
		$this->display ( 'Index:tradelist');
		
		 
	}
	
	
	function t2 () {
		$str = 'a:23:{s:10:"buyer_nick";s:9:"琪漆520";s:7:"created";s:19:"2013-09-05 12:33:32";s:3:"num";i:1;s:6:"orders";a:1:{s:5:"order";a:2:{i:0;a:26:{s:10:"adjust_fee";s:4:"0.00";s:10:"buyer_rate";b:0;s:3:"cid";i:162104;s:12:"consign_time";s:19:"2013-09-05 16:48:11";s:12:"discount_fee";s:5:"89.00";s:8:"end_time";s:19:"2013-09-09 17:03:14";s:10:"invoice_no";s:12:"668576492698";s:10:"is_daixiao";b:0;s:17:"logistics_company";s:13:"申通E物流";s:3:"num";i:1;s:7:"num_iid";i:19023157556;s:3:"oid";i:416280251300860;s:9:"outer_iid";s:1:"A";s:12:"outer_sku_id";s:13:"A绿紫格XXL";s:7:"payment";s:5:"49.00";s:8:"pic_path";s:87:"http://img04.taobaocdn.com/bao/uploaded/i4/372760180/T2ByorXbpaXXXXXXXX_!!372760180.jpg";s:5:"price";s:6:"138.00";s:13:"refund_status";s:9:"NO_REFUND";s:11:"seller_rate";b:1;s:11:"seller_type";s:1:"C";s:13:"shipping_type";s:7:"express";s:6:"sku_id";s:11:"35121291697";s:19:"sku_properties_name";s:34:"颜色分类:A绿紫格;尺码:XXL";s:6:"status";s:14:"TRADE_FINISHED";s:5:"title";s:90:"女格子衫中长款韩版格子衬衫长袖韩版秋冬衬衣修身纯棉外套包邮新";s:9:"total_fee";s:5:"49.00";}i:1;a:26:{s:10:"adjust_fee";s:4:"0.00";s:10:"buyer_rate";b:1;s:3:"cid";i:162104;s:12:"consign_time";s:19:"2013-08-22 23:38:30";s:12:"discount_fee";s:6:"100.00";s:8:"end_time";s:19:"2013-08-28 14:10:46";s:10:"invoice_no";s:12:"668576121684";s:10:"is_daixiao";b:0;s:17:"logistics_company";s:13:"申通E物流";s:3:"num";i:1;s:7:"num_iid";i:19023157556;s:3:"oid";i:404424147520860;s:9:"outer_iid";s:1:"A";s:12:"outer_sku_id";s:11:"A蓝绿格L";s:7:"payment";s:5:"38.00";s:8:"pic_path";s:87:"http://img04.taobaocdn.com/bao/uploaded/i4/372760180/T2sFUrXiNaXXXXXXXX_!!372760180.jpg";s:5:"price";s:6:"138.00";s:13:"refund_status";s:9:"NO_REFUND";s:11:"seller_rate";b:1;s:11:"seller_type";s:1:"C";s:13:"shipping_type";s:7:"express";s:6:"sku_id";s:11:"35121291713";s:19:"sku_properties_name";s:32:"颜色分类:A蓝绿格;尺码:L";s:6:"status";s:14:"TRADE_FINISHED";s:5:"title";s:88:"【淘宝清仓】韩版格子衬衫女长袖2013秋长款衬衣修身纯棉外套包邮";s:9:"total_fee";s:5:"38.00";}}}s:7:"payment";d:87;s:8:"pic_path";s:87:"http://img04.taobaocdn.com/bao/uploaded/i4/372760180/T2ByorXbpaXXXXXXXX_!!372760180.jpg";s:8:"post_fee";s:18:"0.00;<br>0.00;<br>";s:16:"receiver_address";s:67:"纸坊熊廷弼路92号 武汉市江夏区国土资源和规划局";s:13:"receiver_city";s:9:"武汉市";s:17:"receiver_district";s:9:"江夏区";s:15:"receiver_mobile";s:11:"13114388966";s:13:"receiver_name";s:9:"干海燕";s:14:"receiver_phone";s:12:"027-87910952";s:14:"receiver_state";s:9:"湖北省";s:12:"receiver_zip";s:6:"430200";s:11:"seller_flag";i:0;s:11:"seller_nick";s:9:"star18188";s:13:"shipping_type";s:22:"快递;<br>快递;<br>";s:6:"status";s:14:"TRADE_FINISHED";s:3:"tid";i:416280251300860;s:13:"buyer_message";s:0:"";s:11:"seller_memo";s:0:"";s:4:"tids";s:31:"416280251300860,404424147520860";}';
		$t = unserialize( $str );
		var_dump( $t );
		
		print_r( $_SESSION );
		
	
	}
}